package com.lzy.cloud.base.controller;

import com.github.pagehelper.PageInfo;
import com.lzy.cloud.base.dto.ProtocolDto;
import com.lzy.cloud.base.dto.ShopBankDto;
import com.lzy.cloud.base.facade.ProtocolFacade;
import com.lzy.cloud.base.request.ProtocolRequest;
import com.lzy.cloud.base.request.ShopBankRequest;
import com.lzy.cloud.base.response.ProtocolResponse;
import com.lzy.cloud.base.response.ShopBankResponse;
import com.lzy.cloud.base.response.UserToken;
import com.lzy.cloud.base.service.ProtocolService;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 协议
 *
 * @author lc
 * @version 2018/8/17
 */
@RestController
@RequestMapping("/protocol")
public class ProtocolController {

    @Autowired
    private ProtocolFacade protocolFacade;

    @Autowired
    private ProtocolService protocolService;

    @Autowired
    private BaseController baseController;

    /**
     * 添加协议@RequestHeader("token")String token,
     * @param request
     * @return
     */
    @RequestMapping(value = "/create",method = RequestMethod.POST)
    public Result create( @RequestBody ProtocolRequest request) {
        Ensure.that(request).isNotNull("00000002");
//        Result userLogin = baseController.isUserLogin(token);
//        UserToken userToken = DozerUtils.map(userLogin.getResult(), UserToken.class);
//        request.setShopId(userToken.getPlatformId());
        Long id = protocolFacade.create(DozerUtils.map(request, ProtocolDto.class));
        Ensure.that(id).isGt(0, "00030002");
        return new Result(id);
    }

    /**
     * 分页查询
     * @param request
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result selectList(ProtocolRequest request) {
        Ensure.that(request).isNotNull("00000002");
        Pager pager = new Pager();
        pager.setPageNum(request.getPageNum());
        pager.setPageSize(request.getPageSize());
        PageInfo<ProtocolDto> pageInfo = protocolFacade.selectList(pager, DozerUtils.map(request, ProtocolDto.class));
        pager.setTotal(Long.valueOf(pageInfo.getList().size()));
        return new Result(pager,DozerUtils.maps(pageInfo.getList(),ProtocolResponse.class));
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @RequestMapping(value = "/find/{id}", method = RequestMethod.GET)
    public Result find(@PathVariable("id") Long id) {
        Ensure.that(id).isNotNull("00000002");
        ProtocolDto protocolDto= protocolService.find(id);
        Ensure.that(protocolDto).isNotNull("00030006");
        return new Result(DozerUtils.map(protocolDto,ProtocolResponse.class));
    }

    /**
     * 编辑
     * @param request
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result update( @RequestBody ProtocolRequest request) {
        Ensure.that(request).isNotNull("00000002");
//        Result userLogin = baseController.isUserLogin(token);
//        UserToken userToken = DozerUtils.map(userLogin.getResult(), UserToken.class);
//        request.setShopId(userToken.getPlatformId());
        Ensure.that(request.getId()).isNotNull("00000002");
        Boolean ok = protocolFacade.update(DozerUtils.map(request, ProtocolDto.class));
        return new Result(ok);
    }
}
